//
//  FirstViewController.m
//  WeiMo
//
//  Created by weixian he on 12-8-18.
//  Copyright (c) 2012年 . All rights reserved.
//

#import "FirstViewController.h"
#import "ThreeImageCell.h"
#import "UIImageView+WebCache.h"
#import "SVPullToRefresh.h"
#import <QuartzCore/QuartzCore.h>

#import "ALAsset+AGIPC.h"
#import "TwoImageCell.h"
#import "DLog.h"
#import "KeyValueObject.h"
#import "RequestViewController.h"
#import "SecondViewController.h"
#import "SDImageCache.h"

#define kImageViewTagBase 10
#define kImageIntNums 4

typedef enum
{
    CheckNormal,  //普通状态
    CheckIn,      //选择状态
    CheckOK       //完成
}CheckStyle;

@interface FirstViewController ()
{
    CheckStyle mCheckStyle;
}
@property(nonatomic,retain) UITableView *tableView;
@property(nonatomic,retain) UIView *headerView;

@end

@implementation FirstViewController
@synthesize list = _list;
@synthesize tableView = _tableView;
@synthesize headerView = _headerView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"我的模库", @"我的模库");
        self.tabBarItem.image = [UIImage imageNamed:@"ku"];
    }
    return self;
}
							
- (void)viewDidLoad
{
    [super viewDidLoad];
    //self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    //UIImage *image = [UIImage imageNamed:@"bg.png"];
    

    [self testList];
    mCheckStyle = CheckNormal;
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"预约" style:UIBarButtonItemStyleBordered target:self action:@selector(checkIn:)];
    rightButtonItem.tag = 1001;
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    
    CGRect fullRect=[[UIScreen mainScreen] bounds];  //全屏的
    CGRect rect = self.view.frame;
    rect.size.height = fullRect.size.height - 20-44-49;
    self.tableView = [[[UITableView alloc] initWithFrame:rect] autorelease];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    //[self.tableView setBackgroundColor:[UIColor colorWithPatternImage: image]];
    self.tableView.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableView];

    rect = CGRectMake(0, 0, 320, 60);
    self.headerView = [[[UIView alloc] initWithFrame:rect] autorelease];
    rect = CGRectMake(8, 3, 304, 54);
    UITextView *textField = [[[UITextView alloc] initWithFrame:rect] autorelease];
    textField.text = @"输入您需要更新的模特要求，系统会在3天内更新到您的模库。";
    textField.delegate = self;
    textField.font = [UIFont systemFontOfSize:14];
    textField.textColor = [UIColor lightGrayColor];
    textField.layer.cornerRadius = 6;
    textField.layer.borderWidth = 2;
    textField.layer.borderColor = [UIColor lightGrayColor].CGColor; //[UIColor colorWithWhite:.8 alpha:1].CGColor;
    [self.headerView addSubview:textField];
    
    self.headerView.backgroundColor =[UIColor underPageBackgroundColor];
    self.view.backgroundColor = [UIColor scrollViewTexturedBackgroundColor];
    
    /*
    self.list = [[[NSMutableArray alloc] initWithObjects:
                  @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308",
                  @"http://static2.dmcdn.net/static/video/935/132/44231539:jpeg_preview_small.jpg?20120426115744",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",
                  @"http://static2.dmcdn.net/static/video/775/942/44249577:jpeg_preview_small.jpg?20120425011228",nil] autorelease];
     */
    
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.list = nil;
    self.tableView = nil;
    self.headerView = nil;
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(void)dealloc
{
    self.list = nil;
    self.tableView = nil;
    self.headerView = nil;
    [super dealloc];
}

-(void)didReceiveMemoryWarning
{
    DEnter();
    [[SDImageCache sharedImageCache] clearMemory];
    [super didReceiveMemoryWarning];
}

#pragma mark -private
-(void)checkIn:(id)sender
{
    UIBarButtonItem *item = (UIBarButtonItem *)sender;
    if(item.tag == 1001) //预约
    {
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"确定" style:UIBarButtonItemStyleDone target:self action:@selector(checkIn:)];
        rightButtonItem.tag = 1002;
        self.navigationItem.rightBarButtonItem = rightButtonItem;
        [rightButtonItem release];
        
        
        UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"取消" style:UIBarButtonItemStyleBordered target:self action:@selector(cancel:)];
        
        self.navigationItem.leftBarButtonItem = backButton;
        [backButton release];
        
        mCheckStyle = CheckIn;
        CGRect rect = CGRectMake(0, 0, 0, 0);
        self.headerView.frame = rect;
        [self.tableView reloadData];
    }
    else if(item.tag == 1002){
        
        DLog(@"xxx");
        RequestViewController *requestCtrl = [[RequestViewController alloc] init];
        [requestCtrl initWithStyle:UITableViewStyleGrouped];
        //requestCtrl.delegate = self;
        [self.navigationController pushViewController:requestCtrl animated:YES];
        [requestCtrl release];
        
        /*
        UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"预约" style:UIBarButtonItemStyleBordered target:self action:@selector(checkIn:)];
        rightButtonItem.tag = 1001;
        self.navigationItem.rightBarButtonItem = rightButtonItem;
        [rightButtonItem release];
        self.navigationItem.leftBarButtonItem = nil;
        
        mCheckStyle = CheckOK;
        for(KeyValueObject * item in self.list)
        {
            item.isCheck = NO;
        }
        CGRect rect = CGRectMake(0, 0, 320, 60);
        self.headerView.frame = rect;
        [self.tableView reloadData];
        */
    }
}

-(void)cancel:(id)sender
{
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"预约" style:UIBarButtonItemStyleBordered target:self action:@selector(checkIn:)];
    rightButtonItem.tag = 1001;
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    self.navigationItem.leftBarButtonItem = nil;
    
    mCheckStyle = CheckNormal;
    for(KeyValueObject * item in self.list)
    {
        item.isCheck = NO;
    }
    
    CGRect rect = CGRectMake(0, 0, 320, 60);
    self.headerView.frame = rect;
    [self.tableView reloadData];
}



#pragma mark - Table view data source

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        return self.headerView;
    }
    
    return nil;
}
-(float)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 82;
 
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 2;
}

-(float)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if(section == 0)
    {
        if(self.headerView)
        {
            return self.headerView.frame.size.height;
        }
    }
    
    return 0;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(section == 0)
    {
        return 0;
    }
    else if(section == 1)
    {
        int mol = self.list.count % kImageIntNums;
        int size = ([self.list count] )/kImageIntNums + 1;
        if(mol == 0)
        {
            size--;
        }
        return size;
    }
    
    return 0;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.section == 1)
    {
        static NSString *CellIdentifier4 = @"Cell4";
        ThreeImageCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier4];
        if(cell == nil)
        {
            cell = [[[ThreeImageCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier4 canCheck:YES] autorelease];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        
        //[cell setBackgroundColor: [UIColor redColor] ];
        int row = [indexPath row];
        int index;
        int listSize = [self.list count] - 1;
        for( int i = 0; i < kImageIntNums; i++)
        { 
            index = row *kImageIntNums +i;
            TouchImageView *imageView = [cell.imageViews objectAtIndex:i];
            if(index <= listSize)
            {
                KeyValueObject *item = [self.list objectAtIndex:index];
                /*
                NSString *imageUrl = item.keyDispName;
                [imageView setImageWithURL:[NSURL URLWithString:imageUrl] 
                          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
                 */
                [imageView setImage:[UIImage imageNamed:item.keyDispName]];
                
                imageView.delegate = self;
                imageView.tag = index + kImageViewTagBase;
                imageView.hidden = NO;
                
                UIImageView * checkImageView = (UIImageView*)[imageView viewWithTag:kCanCheckTag];
                if(mCheckStyle == CheckIn)
                {
                    checkImageView.hidden = NO;
                    if(item.isCheck == NO)
                    {
                        [checkImageView setImage:[UIImage imageNamed:@"uncheck.png"]];
                    }
                    else {
                        [checkImageView setImage:[UIImage imageNamed:@"check.png"]];
                    }
                }
                else {
                    checkImageView.hidden = YES;
                }
            }
            else 
            {
                [imageView setImageWithURL:nil 
                          placeholderImage:[UIImage imageNamed:@"placeholder.png"]];
                imageView.delegate = nil;
                imageView.hidden = YES;
            }
        }
        return cell;
    }
    else {
        return nil;
    }
   
}

#pragma makr - private
-(void)testList
{
    NSMutableArray *array = [[[NSMutableArray alloc] init] autorelease];
    /*
     canClick:推荐
     isCheck:预约
     key:用户id
     keyDispName:小图url
     */
    int num = 8;
    for(int i = 1; i<= num; i++)
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.canClick = YES;
        item.isCheck = NO;
        item.key = [NSString stringWithFormat:@"%d",i];
        item.keyDispName = [NSString stringWithFormat:@"h%d.jpg",i];
        [array addObject:item];
    }
    
#if 0
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.canClick = YES;
        item.isCheck = NO;
        item.key = @"001";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.canClick = YES;
        item.isCheck = NO;
        item.key = @"002";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }{
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.canClick = YES;
        item.isCheck = NO;
        item.key = @"003";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.canClick = YES;
        item.isCheck = NO;
        item.key = @"004";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"005";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"005";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"005";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"005";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"005";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/732/523/44325237:jpeg_preview_small.jpg?20120426110308";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/874/098/43890478:jpeg_preview_small.jpg?20120415193608";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/874/098/43890478:jpeg_preview_small.jpg?20120415193608";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/874/098/43890478:jpeg_preview_small.jpg?20120415193608";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/874/098/43890478:jpeg_preview_small.jpg?20120415193608";
        [array addObject:item];
    } {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/874/098/43890478:jpeg_preview_small.jpg?20120415193608";
        [array addObject:item];
    }
    {
        KeyValueObject *item = [[[KeyValueObject alloc] init] autorelease];
        item.key = @"006";
        item.keyDispName = @"http://static2.dmcdn.net/static/video/874/098/43890478:jpeg_preview_small.jpg?20120415193608";
        [array addObject:item];
    }
#endif
    
    self.list = array;
}

#pragma mark - TouchImageViewDelegate
-(void)clickImageView:(TouchImageView *)imageView
{
    DLog(@"tag = %d",(imageView.tag  - kImageViewTagBase));
    int index;
    index = imageView.tag - kImageViewTagBase;
    KeyValueObject *item = [self.list objectAtIndex:index];
    if(mCheckStyle == CheckIn)
    {
        UIImageView *checkView = (UIImageView *)[imageView viewWithTag:kCanCheckTag];
        if(checkView)
        {
            item.isCheck = !item.isCheck;
            if(item.isCheck)
            {
                [checkView setImage:[UIImage imageNamed:@"check.png"]];
            }
            else {
                [checkView setImage:[UIImage imageNamed:@"uncheck.png"]];
            }
        }
    }
    else if(mCheckStyle == CheckNormal)
    {
        if(index == 0)
        {
            SecondViewController *modelCtrl = [[[SecondViewController alloc] init] autorelease];
            modelCtrl.modelName = @"小贤";
            modelCtrl.tempNum = @"5";
            modelCtrl.tempKey = @"1";
            modelCtrl.title = @"模特资料";
            [self.navigationController pushViewController:modelCtrl animated:YES];
        }
        else if(index == 1)
        {
            SecondViewController *modelCtrl = [[[SecondViewController alloc] init] autorelease];
            modelCtrl.modelName = @"燕美";
            modelCtrl.tempNum = @"11";
            modelCtrl.tempKey = @"2";
            modelCtrl.title = @"模特资料";
            [self.navigationController pushViewController:modelCtrl animated:YES];
        }
        else if(index == 2)
        {
            SecondViewController *modelCtrl = [[[SecondViewController alloc] init] autorelease];
            modelCtrl.modelName = @"莹莹";
            modelCtrl.tempNum = @"7";
            modelCtrl.tempKey = @"3";
            modelCtrl.title = @"模特资料";
            [self.navigationController pushViewController:modelCtrl animated:YES];
        }
    }

}

-(void) viewListing:(TouchImageView *) imageView
{
    
}

-(void) editListing:(TouchImageView *) imageView
{
    
}

#pragma mark -
- (BOOL)textViewShouldBeginEditing:(UITextView *)textView
{
    DLog(@"xxx");
    RequestViewController *requestCtrl = [[RequestViewController alloc] init];
    [requestCtrl initWithStyle:UITableViewStyleGrouped];
    [self.navigationController pushViewController:requestCtrl animated:YES];
    [requestCtrl release];
    return NO;
}

#pragma mark - RequestViewControllerDelegate
-(void)comiOK
{
    UIBarButtonItem *rightButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"预约" style:UIBarButtonItemStyleBordered target:self action:@selector(checkIn:)];
    rightButtonItem.tag = 1001;
    self.navigationItem.rightBarButtonItem = rightButtonItem;
    [rightButtonItem release];
    self.navigationItem.leftBarButtonItem = nil;
    
    mCheckStyle = CheckOK;
    for(KeyValueObject * item in self.list)
    {
        item.isCheck = NO;
    }
    CGRect rect = CGRectMake(0, 0, 320, 60);
    self.headerView.frame = rect;
    [self.tableView reloadData];
}
@end